package tr.edu.metu.is.mega.client;

import tr.edu.metu.is.mega.client.core.DialogCallback;
import tr.edu.metu.is.mega.client.resource.MEGAResourceBundle;
import tr.edu.metu.is.mega.client.resource.MEGAResources;
import tr.edu.metu.is.mega.client.service.GwtMailService;
import tr.edu.metu.is.mega.common.UserNotFoundException;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Login dialog.
 */
public class LoginDialog extends ContentPanel {

    private TextField<String> textFieldUser;

    private TextField<String> textFieldPassword;

    private DialogCallback<LoginParameters> callback;

    private final MEGAResourceBundle resources = MEGAResources.RESOURCEBUNDLE;

    private FormPanel form;

    /**
     * Default constructor.
     */
    public LoginDialog() {
        setLayout(new FitLayout());
        setHeading(resources.logindialog_title());
        setSize(300, 150);
        initialize();
    }

    /**
     * Initializes this.
     */
    private void initialize() {
        form = new FormPanel();
        form.setHeaderVisible(false);
        form.setLabelWidth(100);
        Text txtNewText = new Text(" ");
        form.add(txtNewText, new FormData("100%"));
        textFieldUser = new TextField<String>();
        textFieldUser.setAllowBlank(false);
        textFieldUser.setMaxLength(20);
        form.add(textFieldUser, new FormData("98%"));
        textFieldUser.setFieldLabel(resources.logindialog_textfielduserid_label());

        textFieldPassword = new TextField<String>();
        textFieldPassword.setPassword(true);
        textFieldPassword.setAllowBlank(false);
        textFieldPassword.setMaxLength(20);
        form.add(textFieldPassword, new FormData("98%"));
        textFieldPassword.setFieldLabel(resources.logindialog_textfieldpassword_label());

        LayoutContainer panelButton = new LayoutContainer();
        panelButton.setLayout(new CenterLayout());
        Button buttonLogin = new Button(resources.logindialog_buttonlogin_label());
        buttonLogin.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                doLogin();
            }
        });

        Button forgotButton = new Button("Forgot username");
        forgotButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

            @Override
            public void componentSelected(ButtonEvent ce) {
                MessageBox.prompt("Forgot password", "Enter email address associated with your MEGA Account", new Listener<MessageBoxEvent>() {

                    @Override
                    public void handleEvent(MessageBoxEvent be) {
                        GwtMailService.Util.getInstance().sendLoginInfo(be.getValue(), new AsyncCallback<Void>() {

                            @Override
                            public void onSuccess(Void result) {
                                Info.display("", "Mail send, check your e-mail");
                            }

                            @Override
                            public void onFailure(Throwable caught) {
                                if (caught instanceof UserNotFoundException) {
                                    Info.display("", "User with given e-mail not found...");
                                } else {
                                    Info.display("", "Sending mail failed");
                                }
                            }
                        });
                    }
                });
            }
        });

        panelButton.add(buttonLogin);
        panelButton.add(forgotButton);
        form.add(panelButton, new FormData("100%"));
        panelButton.setHeight("25");

        this.add(form);

    }

    /**
     * Shows dialog for login.
     * 
     * @param cb dialog callback
     */
    public void login(DialogCallback<LoginParameters> cb) {
        this.callback = cb;
        this.show();
    }

    /**
     * Handles login.
     */
    private void doLogin() {
        if (!form.isValid()) {
            return;
        }
        this.hide();
        LoginParameters params = new LoginParameters();
        params.setUserID(textFieldUser.getValue());
        params.setPassword(textFieldPassword.getValue());
        callback.onOk(params);
    }

}
